package it.trekker.gps.service;

import java.util.ArrayList;
import java.util.List;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

public class GPSLocationService extends Service implements LocationListener, GpsStatus.Listener {
	
	private static final String TAG = GPSLocationService.class.getSimpleName();
	
	private static final int GPS_MINTIME = 4000;	// milliseconds
	private static final int GPS_MINDISTANCE = 2;	// meters
	
	private List<IGPSListener> gpsListeners = null;
	private LocationManager locationManager = null;
	private int gpsStatus;
	
	private final IBinder mBinder = new GPSBinder();
	
	
	/* Binder class for client */
	public class GPSBinder extends Binder {
		public GPSLocationService getService() {
			return GPSLocationService.this;
		}
	} 
	
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		Log.d(TAG, "[SERVICE] onCreate");
		
		gpsListeners = new ArrayList<IGPSListener>();
		
		// Load preferences
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
		int minTime = Integer.parseInt(pref.getString("pref_gps_mintime", String.valueOf(GPS_MINTIME)));
		int minDistance = Integer.parseInt(pref.getString("pref_gps_mindistance", String.valueOf(GPS_MINDISTANCE)));
		
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, this);
		locationManager.addGpsStatusListener(this);
	}
	
	@Override
    public void onStart(Intent intent, int startId) {
        Log.d(TAG, "[SERVICE] onStart");
        
        super.onStart(intent, startId);
    }
	
	@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "[SERVICE] Received start id " + startId + ": " + intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }

	@Override
	public void onDestroy() {
		Log.d(TAG, "[SERVICE] onDestroy");
		
		super.onDestroy();
		
		locationManager.removeUpdates(this);
		locationManager.removeGpsStatusListener(this);
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		Log.d(TAG, "[SERVICE] onBind");
		
		return mBinder;
	}
	
	
	public void addGPSListener(IGPSListener listener) {
		Log.d(TAG, "[SERVICE] addGPSListener");
		
		gpsListeners.add(listener);
	}
	
	public void removeGPSListener(IGPSListener listener) {
		Log.d(TAG, "[SERVICE] removeGPSListener");
		
		gpsListeners.remove(listener);
	}

	@Override
	public void onLocationChanged(Location location) {
		Log.d(TAG, "[SERVICE] onLocationChanged");
		
		for(IGPSListener listener : gpsListeners) {
			listener.onLocationChanged(location);
		}
	}

	@Override
	public void onProviderDisabled(String provider) {
		for(IGPSListener listener : gpsListeners) {
			listener.onProviderDisabled();
		}		
	}

	@Override
	public void onProviderEnabled(String provider) {
		for(IGPSListener listener : gpsListeners) {
			listener.onProviderEnabled();
		}	
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		if(provider.equals(LocationManager.GPS_PROVIDER)) {
			for(IGPSListener listener : gpsListeners) {
				listener.onStatusChanged(status);
			}
		}
	}


	@Override
	public void onGpsStatusChanged(int event) {
//		Log.d(TAG, "onGpsStatusChanged: " + event);
		
		gpsStatus = event;
	}
}
